package com.sast.manage_backend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sast.manage_backend.common.Response;
import com.sast.manage_backend.model.TempHumid;
import com.sast.manage_backend.service.TempHumidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/tempHumid")
public class TempHumidController {
    @Autowired
    private TempHumidService tempHumidService;


    @GetMapping("/getByDeviceId/{id}")
    public Response<?> getByDeviceId(@PathVariable short id) {
        TempHumid tempHumid = tempHumidService.getTempHumidByDeviceId(id);
        if (tempHumid == null) {
            return Response.newError("设备不存在");
        }
        else {
            return Response.newSuccess(tempHumidService.getTempHumidByDeviceId(id));
        }
    }

    @GetMapping("/getAll")
    public Response<List<TempHumid>> getAll() {
        List<TempHumid> tempHumidList= new ArrayList<>();
        short deviceId = 1;
        while(true) {
            TempHumid tempHumid = tempHumidService.getTempHumidByDeviceId(deviceId);
            if (tempHumid == null) {
                break;
            }
            tempHumidList.add(tempHumid);
            deviceId += 1;
        }
        return Response.newSuccess(tempHumidList);
    }



}
